package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Course;
import com.ruoyi.system.domain.Link;
import com.ruoyi.system.service.ICourseService;
import com.ruoyi.system.service.ILinkService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PerformanceMapper;
import com.ruoyi.system.domain.Performance;
import com.ruoyi.system.service.IPerformanceService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 学生成绩Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-23
 */
@Service
public class PerformanceServiceImpl implements IPerformanceService 
{
    @Resource
    private PerformanceMapper performanceMapper;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ICourseService courseService;

    @Resource
    private ILinkService linkService;

    /**
     * 查询学生成绩
     * 
     * @param id 学生成绩主键
     * @return 学生成绩
     */
    @Override
    public Performance selectPerformanceById(Long id)
    {
        return performanceMapper.selectPerformanceById(id);
    }

    /**
     * 查询学生成绩列表
     * 
     * @param performance 学生成绩
     * @return 学生成绩
     */
    @Override
    public List<Performance> selectPerformanceList(Performance performance)
    {
        List<Long> roleIds = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList());
        // 学生
        if (roleIds.contains(100L)) {
            performance.setState("审核通过");
            performance.setUserId(SecurityUtils.getUserId());
        }
        // 教师如果是教师的话 只能看到自己教的课程的成绩
        if (roleIds.contains(101L)) {
            Link link = new Link();
            link.setUserId(SecurityUtils.getUserId());
            List<Link> links = linkService.selectLinkList(link);
            if (!CollectionUtils.isEmpty(links)) {
                performance.setCourseIds(links.stream().map(Link::getCourseId).collect(Collectors.toList()));
            }

        }
        return performanceMapper.selectPerformanceList(performance);
    }

    /**
     * 新增学生成绩
     * 
     * @param performance 学生成绩
     * @return 结果
     */
    @Override
    public int insertPerformance(Performance performance)
    {
        rich(performance);
        performance.setCreateTime(DateUtils.getNowDate());
        return performanceMapper.insertPerformance(performance);
    }

    /**
     * 丰富数据
     * @param performance 成绩信息
     */
    public void rich(Performance performance) {
        // 查询学生信息
        if (performance.getUserId() == null) {
            throw new ServiceException("学生信息不能为空");
        }
        SysUser user = sysUserService.selectUserById(performance.getUserId());
        // 查询课程信息
        if (performance.getCourseId() == null) {
            throw new ServiceException("课程信息不能为空");
        }
        Course course = courseService.selectCourseById(performance.getCourseId());
        BigDecimal usualPerformance = performance.getUsualPerformance();
        if (new BigDecimal(100).compareTo(usualPerformance) < 0) {
            throw new ServiceException("分数要在0 - 100区间范围内");
        }
        BigDecimal courseGrade = performance.getCourseGrade();
        if (new BigDecimal(100).compareTo(courseGrade) < 0) {
            throw new ServiceException("分数要在0 - 100区间范围内");
        }
        if (new BigDecimal(0).compareTo(usualPerformance) > 0) {
            throw new ServiceException("分数要在0 - 100区间范围内");
        }
        if (new BigDecimal(0).compareTo(courseGrade) > 0) {
            throw new ServiceException("分数要在0 - 100区间范围内");
        }
        // 计算比率 平时分
        BigDecimal pacific = usualPerformance.multiply(course.getPacificRatio()).setScale(2, RoundingMode.DOWN);
        // 计算比率 课程分数
        BigDecimal courseo = courseGrade.multiply(course.getCourseRatio()).setScale(2, RoundingMode.DOWN);
        // 总分
        performance.setTotalPoints(pacific.add(courseo));
        performance.setUserName(user.getUserName());
        performance.setCourseName(course.getName());
        performance.setSemesterId(course.getSemesterId());
        performance.setSemesterName(course.getSemesterName());
        performance.setState("待审核");
    }


    /**
     * 修改学生成绩
     * 
     * @param performance 学生成绩
     * @return 结果
     */
    @Override
    public int updatePerformance(Performance performance)
    {
        rich(performance);
        performance.setState("待审核");
        performance.setUpdateTime(DateUtils.getNowDate());
        return performanceMapper.updatePerformance(performance);
    }

    /**
     * 审核按钮
     * @param performance 审核
     * @return res
     */
    @Override
    public int authPerformance(Performance performance) {
        performance.setUpdateTime(DateUtils.getNowDate());
        return performanceMapper.updatePerformance(performance);
    }

    /**
     * 批量删除学生成绩
     * 
     * @param ids 需要删除的学生成绩主键
     * @return 结果
     */
    @Override
    public int deletePerformanceByIds(Long[] ids)
    {
        return performanceMapper.deletePerformanceByIds(ids);
    }

    /**
     * 删除学生成绩信息
     * 
     * @param id 学生成绩主键
     * @return 结果
     */
    @Override
    public int deletePerformanceById(Long id)
    {
        return performanceMapper.deletePerformanceById(id);
    }
}
